package com.feicheng.controller.wx;

import com.alibaba.fastjson.JSONObject;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Client;
import com.feicheng.persistence.service.IClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController("/wx/client")
@RequestMapping("/wx/client")
public class ClientController {

    @Autowired
    IClientService clientService;

    @PutMapping("/info")
    public ResultInfo updateClient(@RequestBody Client client) {

        if (StringUtils.hasLength(client.getHeadimg()) && (client.getHeadimg().startsWith("http:") || client.getHeadimg().startsWith("wxfile:"))) {
            client.setHeadimg(null);
        }

        clientService.updateById(client);

        return ResultInfo.success(clientService.getById(client.getClientid()));
    }

    @GetMapping("/info")
    public ResultInfo getClient(String clientid) {
        Client client = clientService.getById(clientid);
        JSONObject result = new JSONObject();
        result.put("client", client);
        return ResultInfo.success(result);
    }
}
